package com.kcs.fxmargin.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.OIDGenerator;
import com.kcs.fxmargin.bo.CustomerInformationBO;
import com.kcs.fxmargin.dao.CustInfoDao;
import com.kcs.fxmargin.model.CCustStatus;
import com.kcs.fxmargin.model.MCustInfo;

public class CustInfoService {
	
	CustInfoDao dao = new CustInfoDao();

	public List<CustomerInformationBO> searchCustomerInfo( MCustInfo custInfo) throws Exception{
		Connection conn 						= null;
		PreparedStatement pstm 					= null;
		ResultSet rs							= null;
		List<CustomerInformationBO> custList 	= new ArrayList<CustomerInformationBO>();
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			
			custList = dao.searchCustomerInfo(conn, pstm, rs, custInfo);
			
		} catch (Exception e){
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return custList;
	}
	
	public List<CustomerInformationBO> searchPopupCustomer( MCustInfo custInfo) throws Exception{
		Connection conn 						= null;
		PreparedStatement pstm 					= null;
		ResultSet rs							= null;
		List<CustomerInformationBO> custList 	= new ArrayList<CustomerInformationBO>();
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			
			custList = dao.searchPopupCustomer(conn, pstm, rs, custInfo);
			
		} catch (Exception e){
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return custList;
	}
	
	public String insertCustInfo(MCustInfo custInfo) throws Exception {
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		String result			= "";
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			
			custInfo.setId(OIDGenerator.create());
			
			result = dao.insertCustomerInfo(conn, pstm, custInfo);
			conn.commit();
		} catch (Exception e){
			result = "e";
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
		
		return result;
	}
	
	public String updateCustInfo(MCustInfo custInfo) throws Exception {
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		String result			= "";
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
						
			result = dao.updateCustomerInfo(conn, pstm, custInfo);
			conn.commit();
		} catch (Exception e){
			result = "e";
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
		
		return result;
	}
	
	public String delete(String id) throws Exception {
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		String result			= "";
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
						
			result = dao.delete(conn, pstm, id);
			conn.commit();
		} catch (Exception e){
			result = "e";
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
		
		return result;
	}
	
	public List<CCustStatus> getCustomerStatus() throws Exception {
		Connection conn 						= null;
		PreparedStatement pstm 					= null;
		ResultSet rs							= null;
		List<CCustStatus> statusList	= new ArrayList<CCustStatus>();
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			
			statusList = dao.getCustomerStatus(conn, pstm, rs);
			
		} catch (Exception e){
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return statusList;
	}
}
